/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import JDBCUtil.MyConn;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import model.admin.Country;
import model.admin.Feedback;

/**
 *
 * @author TuanAnh
 */
public class FeedbackManager {

    Connection conn;

    public FeedbackManager() {
        try {
            conn = MyConn.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Feedback> getAll() {
        List<Feedback> list = new ArrayList<Feedback>();
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM FeedBacks");
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Feedback f = new Feedback();
                f.setFeebackId(rs.getInt("Feedback_Id"));
                f.setName(rs.getNString("Name"));
                f.setEmail(rs.getNString("Email"));
                f.setFeebackTitle(rs.getNString("Feedback_Title"));
                f.setFeebackText(rs.getString("Feedback_Text"));
                f.setCreatedTime(rs.getDate("Created_Time"));
                f.setFeedbackStatus(rs.getInt("Feedback_Status"));
                list.add(f);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public Feedback getOne(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM FeedBacks WHERE Feedback_Id = ?");
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            Feedback f = new Feedback();
            f.setFeebackId(rs.getInt("Feedback_Id"));
            f.setName(rs.getNString("Name"));
            f.setEmail(rs.getNString("Email"));
            f.setFeebackTitle(rs.getNString("Feedback_Title"));
            f.setFeebackText(rs.getString("Feedback_Text"));
            f.setCreatedTime(rs.getDate("Created_Time"));
            f.setFeedbackStatus(rs.getInt("Feedback_Status"));
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean insert(Feedback f) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO FeedBacks (Name,Email,Feedback_Title,Feedback_Text,Created_Time,Feedback_Status) VALUES (?,?,?,?,?,?)");
            pstmt.setNString(1, f.getName());
            pstmt.setNString(2, f.getEmail());
            pstmt.setNString(3, f.getFeebackTitle());
            pstmt.setNString(4, f.getFeebackText());
            pstmt.setDate(5, f.getCreatedTime());
            pstmt.setInt(4, f.getFeedbackStatus());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }
        return false;
    }

    public boolean update(Feedback f) {
        PreparedStatement pstmt;
        try {
            pstmt = conn.prepareStatement("UPDATE FeedBacks SET Name=?,Email=?,Feedback_Title=?,Feedback_Text = ? ,Feedback_Text=?,Created_Time = ?,Feedback_Status = ? WHERE Feedback_Id = ?");
            pstmt.setNString(1, f.getName());
            pstmt.setNString(2, f.getEmail());
            pstmt.setNString(3, f.getFeebackTitle());
            pstmt.setNString(4, f.getFeebackText());
            pstmt.setDate(5, f.getCreatedTime());
            pstmt.setInt(6, f.getFeedbackStatus());
            pstmt.setInt(7, f.getFeebackId());
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delete(int id) {
        try {
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM FeedBacks  WHERE Feedback_Id = ?");
            pstmt.setInt(1, id);
            int eu = pstmt.executeUpdate();
            if (eu > 0) {
                return true;
            }
        } catch (Exception e) {
        }

        return false;
    }
    public static void main(String[] args) {
        FeedbackManager mana=new FeedbackManager();
        System.out.println(mana.getAll().size());
    }
}
